/*
 * Copyright (c) 2010 Network Security Lab (NSL), Department of Electrical
 *                    Engineering, University of Washington
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: He Wu mdzz@uw.edu
 */

/**
 * @file packet_handler.h
 * @brief Packet handler using master/worker model.
 */

#ifndef PACKET_HANDLER
#define PACKET_HANDLER

#include "network.h"

/**
 * Packet queue node.
 */
typedef struct packet_q_node_st {
  packet *pkt;                    //!< pointer to packet
  struct packet_q_node_st *next;  //!< pointer to next node
} packet_q_node;

/**
 * Packet queue (thread safe).
 */
typedef struct packet_q_st {
  packet_q_node *head;  //!< pointer to head of packet queue
  packet_q_node *tail;  //!< pointer to tail of packet queue
  pthread_mutex_t lock; //!< lock associated with packet queue
} packet_q;

/**
 * Task queue of handling packets.
 */
typedef struct task_q_st {
  packet_q *q;          //!< pointer to packet queue
  int destroy;          //!< flag, set when the task queue is to be destroyed
  int num_of_workers;   //!< number of worker threads
  pthread_t *workers;   //!< list of worker threads
  pthread_mutex_t lock; //!< lock
  pthread_cond_t cv;    //!< conditional variable
} task_q;

/**
 * @brief Append a new packet to the end of packet queue.
 * @param q Pointer to the packet queue.
 * @param pkt Pointer to the packet to be appended.
 */
void packet_q_append(packet_q *q, packet *pkt);

/**
 * @brief Remove a packet from the packet queue.
 * @param q Pointer to the packet queue.
 * @return Pointer to the removed packet.
 *
 * The node containing the packet is released from the packet queue, the packet
 * is to be released after it has been processed.
 */
packet *packet_q_remove(packet_q *q);

/**
 * @param q Pointer to the packet queue.
 * @return Size (# of elements) of the packet queue.
 */
int packet_q_size(packet_q *q);

/**
 * @brief Initializes a packet queue.
 * @param Pointer to packet queue.
 */
void packet_q_init(packet_q *q);

/**
 * @brief Destroy a packet queue.
 * @param q Pointer to packet queue.
 */
void packet_q_destroy(packet_q *q);

/**
 * @brief Create a task queue.
 * @param tq Pointer to task queue.
 * @param num_of_workers Number of workers.
 * @return 0 of all is good.
 */
int task_queue_init(task_q *tq, int num_of_workers);

/**
 * @brief Destroy the task queue.
 * @param tq Pointer to task queue.
 */
void destroy_task_queue(task_q *tq);

/**
 * @brief Add task(packet) to task queue.
 * @param tq Pointer to task queue.
 * @param pkt Pointer to packet.
 *
 * In the number of workers is 0 in the task queue, the packet is processed by
 * this function.
 */
void add_task(task_q *tq, packet *pkt);

#endif
